package com.lzy.wzy.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.wzy.aspect.Api;
import com.lzy.wzy.model.DepartmentBean;
import com.lzy.wzy.model.PermissionBean;
import com.lzy.wzy.model.RoleBean;
import com.lzy.wzy.model.User;
import com.lzy.wzy.result.Result;
import com.lzy.wzy.service.AdminService;
import com.lzy.wzy.utils.PatternUtils;
import com.lzy.wzy.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;


@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {


    @Autowired
    private AdminService adminService;

    //    添加用户
    @Api(value = "添加用户")
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public Result<T> addUser(@Validated @RequestBody UserVo userVo) {
        return adminService.addUser(userVo);
    }


    //获取所有用户
    @Api(value = "获取所有用户")
    @RequestMapping(value = "/getAllUser", method = RequestMethod.GET)
    public Result<PageInfo<User>> getAllUser(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum
            , @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return adminService.queryAllUserInfo(pageSize, pageNum);
    }

    //修改用户信息
    @Api(value = "修改用户信息")
    @RequestMapping(value = "/modifyUserInfo", method = RequestMethod.POST)
    public Result<T> modifyUserInfo(@Validated @RequestBody ModifyUserVo user) {
        return adminService.modifyUserInfo(user);
    }

    //加载管理员页面职位和部门信息
    @Api(value = "加载管理员页面职位和部门信息")
    @RequestMapping(value = "/adminPageInitLoad", method = RequestMethod.GET)
    public Result<AdminPageInitVo> adminPageInitLoad() {
        return adminService.adminPageInitLoad();
    }

    //获取所有角色信息
    @RequestMapping(value = "/getAllRole", method = RequestMethod.GET)
    @Api(value = "获取所有角色信息")
    public Result<List<RoleBean>> getAllRole() {
        return adminService.doGetAllRole();
    }

    //获取所有部门
    @RequestMapping(value = "/getAllDepartment", method = RequestMethod.GET)
    @Api(value = "获取所有部门")
    public Result<List<DepartmentBean>> getAllDepartment() {
        return adminService.doGetAllDepartment();
    }

    //删除用户
    @RequestMapping(value = "/deleteUsers", method = RequestMethod.GET)
    @Api(value = "删除用户")
    public Result<T> deleteUsers(@NotBlank @RequestParam("userUuid") String userUuid) {
        return adminService.deleteUsers(userUuid);
    }

    //多条件查询用户
    @RequestMapping(value = "/multiConditionQueryUser", method = RequestMethod.GET)
    @Api(value = "多条件查询用户")
    public Result<PageInfo<User>> multiConditionQueryUser(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum
            , @RequestParam(value = "pageSize", defaultValue = "15") int pageSize, @NotBlank @RequestParam("searchValue") String searchValue) {
        return adminService.doMultiConditionQueryUser(pageSize, pageNum, searchValue.trim());
    }

    //通过角色查询用户
    @RequestMapping(value = "/queryUserInformationByRole", method = RequestMethod.GET)
    @Api(value = "通过角色查询用户")
    public Result<PageInfo<User>> queryUserInformationByRole(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum
            , @RequestParam(value = "pageSize", defaultValue = "15") int pageSize, @NotBlank @RequestParam("roleId") String roleId) {
        return adminService.doQueryUserInformationByRole(pageSize, pageNum, roleId);
    }


    //通过部门查询员工
    @RequestMapping(value = "/queryUserInformationByDepartment", method = RequestMethod.GET)
    @Api(value = "通过部门查询员工")
    public Result<PageInfo<User>> queryUserInformationByDepartment(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum
            , @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, @NotBlank @RequestParam("departmentId") String departmentId) {
        return adminService.doQueryUserInformationByDepartment(pageSize, pageNum, departmentId);
    }

    //添加新的角色
    @RequestMapping(value = "/addNewPosition", method = RequestMethod.POST)
    @Api(value = "添加新的角色")
    public Result<T> addNewPosition(@NotBlank @RequestParam("roleName") String roleName, @NotEmpty @RequestParam(required = false, value = "permissionIds[]") List<Integer> permissionIds, @NotBlank @RequestParam("departmentId") String departmentId) {
        return adminService.doAddNewPosition(roleName, permissionIds, Integer.parseInt(departmentId));
    }

    //添加新部门
    @RequestMapping(value = "/addNewDepartment", method = RequestMethod.POST)
    @Api(value = "添加新部门")
    public Result<T> addNewDepartment(@NotBlank @RequestParam("departmentName") String departmentName) {
        return adminService.doAddNewDepartment(departmentName);
    }

    //获取所有部门
    @RequestMapping(value = "/getPermissionUnderTheDepartment", method = RequestMethod.GET)
    @Api(value = "获取所有部门")
    public Result<List<PermissionBean>> getPermissionUnderTheDepartment(@NotBlank @RequestParam("departmentId") String departmentId) {
        return adminService.doGetPermissionUnderTheDepartment(departmentId);
    }

    //获取此部门下所有权限信息
    @RequestMapping(value = "/getAPositionUnderTheDepartment", method = RequestMethod.GET)
    @Api(value = "获取此部门下所有权限信息")
    public Result<List<RoleBean>> getAPositionUnderTheDepartment(@NotBlank @RequestParam("departmentId") String departmentId) {
        return adminService.doGetAPositionUnderTheDepartment(departmentId);
    }

    //删除角色
    @RequestMapping(value = "/deleteRoleInfo", method = RequestMethod.GET)
    @Api(value = "删除角色")
    public Result<T> deleteRoleInfo(@NotBlank @RequestParam("roleId") String roleId) {
        if (PatternUtils.checkDigitId(roleId)) {
            return Result.invalidFormat();
        }
        return adminService.doDeleteRoleInfo(Integer.parseInt(roleId));
    }

    //重置密码
    @RequestMapping(value = "/resetPassword", method = RequestMethod.GET)
    @Api(value = "重置密码")
    public Result<T> resetPassword(@NotBlank @RequestParam("userId") String userId) {
        return adminService.doResetPassword(userId);
    }

    //获取用户姓名，账户，角色信息
    @RequestMapping(value = "/getUserPartInfo", method = RequestMethod.GET)
    @Api("获取用户姓名，账户，角色信息")
    public Result<UserPartInfoVo> getUserPartInfo() {
        return adminService.doGetUserPartInfo();
    }


    //获取所有工号
    @RequestMapping(value = "/getAllJobNumber", method = RequestMethod.GET)
    @Api("获取所有工号")
    public Result<List<KeyValueVo>> getAllJobNumber() {
        return adminService.doGetAllJobNumber();
    }

    //禁用用户
    @RequestMapping(value = "/disableUser", method = RequestMethod.GET)
    @Api(value = "禁用用户")
    public Result<Integer> disableUser(@NotBlank @RequestParam("userId") String userId) {
        return adminService.doDisableUser(userId);
    }


    //解封用户
    @RequestMapping(value = "/unLockUser", method = RequestMethod.GET)
    @Api(value = "解封用户")
    public Result<Integer> unLockUser(@NotBlank @RequestParam("userId") String userId) {
        return adminService.doUnlockTheUser(userId);
    }

    //邮箱绑定
    @RequestMapping(value = "/bindingEmail/{email}",method = RequestMethod.GET)
    public Result<T> bindingEmail(@PathVariable("email")String email){
        return adminService.doBingdingEmail(email);
    }
}
